package com.tbit.main.service.impl;

import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.PushPayload.Builder;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
import com.tbit.main.constant.AppPushConstant;
import com.tbit.main.util.LogUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class JPushService {
    /**
     * 通知推送
     * 备注：推送方式不为空时，推送的值也不能为空；推送方式为空时，推送值不做要求
     *
     * @param type  推送方式：1、“tag”标签推送，2、“alias”别名推送 3、 “registrationId”registrationId 推送
     * @param alert 推送的内容
     */
    public static PushResult pushNotice(String type, String value, String alert, String extraMessage, Integer tp, String voice) {
        Builder builder = PushPayload.newBuilder();
        //设置接受的平台，all为所有平台，包括安卓、ios、和微软的
        builder.setPlatform(Platform.all());
        //设置如果用户不在线、离线消息保存的时间
        Options options = Options.sendno();
        //设置为86400为保存一天，如果不设置默认也是保存一天
        options.setTimeToLive(86400L);
        //线上环境
        options.setApnsProduction(true);
        builder.setOptions(options);
        //设置推送方式
        if (type.equals(AppPushConstant.PUSH_TYPE_ALIAS)) {
            //根据别名推送
            builder.setAudience(Audience.alias(value));
        } else if (type.equals(AppPushConstant.PUSH_TYPE_TAG)) {
            //根据标签推送
            builder.setAudience(Audience.tag(value));
        } else if (type.equals(AppPushConstant.PUSH_TYPE_REGID)) {
            //根据registrationId推送
            builder.setAudience(Audience.registrationId(value));
        } else {
            //Audience设置为all，说明采用广播方式推送，所有用户都可以接收到
            builder.setAudience(Audience.all());
        }
        //设置为采用通知的方式发送消息
        Notification.Builder bd = Notification.newBuilder();
        bd.setAlert(alert);
        bd.addPlatformNotification(AndroidNotification.newBuilder()
                .addExtra("type", tp)
                .addExtra("params", extraMessage)
                .addCustom("sound", voice)
                .build());
        bd.addPlatformNotification(IosNotification.newBuilder()
                .setBadge(5)
                .setSound(voice + ".caf")
                .addExtra("type", tp)
                .addExtra("params", extraMessage)
                .build());
        builder.setNotification(bd.setAlert(alert).build());
        builder.setOptions(Options.newBuilder()
                .setApnsProduction(true)
                .build());
        PushPayload pushPayload = builder.build();
        PushResult pushResult = new PushResult();
        try {
            //进行推送，实际推送就在这一步
            pushResult = client().sendPush(pushPayload);
            LogUtil.info("极光推送结果：[" + "reg_id : " + value + " content : " + alert + pushResult + "]");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return pushResult;
    }

    private static JPushClient client() {
        AppPushConstant appPushConstant = new AppPushConstant();
        return new JPushClient(appPushConstant.MASTER_SECRET, appPushConstant.APPKEY);
    }

    /**
     * 安卓与ios平台，通过别名推送
     *
     * @param alias   别名
     * @param content 推送内容
     * @param key     拓展字段key值
     * @param value   拓展字段value
     * @return
     */
    public static PushPayload buildPushObject_all_alias_alert(String alias, String content, String key, String value) {
        return PushPayload.newBuilder()
                .setPlatform(Platform.android_ios())
                .setAudience(Audience.alias(alias))
                .setNotification(Notification.newBuilder()
                        .setAlert(content)
                        .addPlatformNotification(AndroidNotification.newBuilder().addExtra(key, value)
                                .build())
                        .addPlatformNotification(IosNotification.newBuilder()
                                .setBadge(5)
                                .setSound("default").addExtra(key, value)
                                .build())
                        .build())
                .build();
    }

    public static void main(String[] args) {
        String alert = "lmy测试";
        String extraMessage = "content";
        Integer tp = 1;
        pushNotice("registrationId", "141fe1da9e6dd8f8eda", alert, extraMessage, tp, "new_work_order");
    }
}